import axios from "axios"
axios.defaults.baseURL = 'http://192.168.0.108:8086'; //http://my.dkb.com/ http://jcc.tw186.com/
axios.defaults.timeout = 30000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.post['enctype'] = 'multipart/form-data';
let cancel, promiseArr = {}
const CancelToken = axios.CancelToken;
//请求前拦截  这里可以拦截用户的所有请求  请求之前发生   use 有个回调 config
axios.interceptors.request.use(function(config) {
    console.log(config)
    //业务在这里处理console.log(request init)
    //  console.log(/(getMoneyList|my_invite_info|seeBank|bankInfo)/.test(config.url),config.url.indexOf('bankInfo')>0)
    // if(/(addBank|addCredit|bankInfo|choiceBank|CreditCardReceipts|creditProList|delCredit|delBank|editCredit|executeProgram|endProgram|getMoneyList|getPayBlock|getReal|getPrion|withdraw|my_invite_list|my_invite_info|my_profit|userFeilv|seeCredit|seeBank|seeProgram|produceProgram|payTypeBlock|payMoney|payReturn)/.test(config.url))
    // {
    //     const AUTH_TOKEN = localStorage.getItem('Token');
    //     if (AUTH_TOKEN) {
    //         config.headers['userId'] = AUTH_TOKEN
    //     }
        //发起请求时，取消掉当前正在进行的相同请求
        if (promiseArr[config.url]) {
            promiseArr[config.url]('操作取消')
            promiseArr[config.url] = cancel
        } else {
            promiseArr[config.url] = cancel
        };
    // }
    return config;
}, function(error) {
    return Promise.reject(error);
});

//请求后拦截  响应拦截     回调response   响应之后没有拦截return response
axios.interceptors.response.use(function(response) {
    console.log(response)
    if (typeof response.data == 'string' || response.data == 'String') {
        return JSON.parse(response.data); //直接返回数据
    }

    if(response.data.code&&response.data.code == "401"){
        localStorage.clear();
    }
    return response.data; //直接返回数据
}, function(error) {
    if (error.response) {
        switch (error.response.status) {
            case 401:
        }
    }
    return Promise.reject(error.response);
});

//设置请求超时 10s
axios.defaults.timeout = 10000;


export default {
    //get请求
    get(url, param) {
        return axios({
            method: 'get',
            url,
            params: param,
            cancelToken: new CancelToken(c => {
                cancel = c
            })
        })
    },
    //post请求
    post(url, param) {
        return axios({
            method: 'post',
            url,
            data: param,
            cancelToken: new CancelToken(c => {
                cancel = c
            })
        })
    }
}